package CSSapp::Controller::Form;

use strict;
use warnings;
use base 'Catalyst::Controller::HTML::FormFu';

use Carp qw/ croak /;

=head1 NAME

CSSapp::Controller::Form - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 index 

=cut

sub view : Local : Args(2) : Form() {
    my ( $self, $c, $form_name, $css_id ) = @_;

    croak 'form name not known'
        if !grep { $form_name eq $_ } qw/ with-labels no-labels with-errors /;
    
    my $form = $c->stash->{form};
    
    $form->load_config_file(
        $c->config->{root} . "/forms/form/$form_name.yml"
    );

    my $method = $form_name;
    $method =~ s/-/_/g;

    if ( $form_name =~ /-errors$/ && $self->can($method) ) {
        $form->indicator( sub { 1 } );
        my $query = $self->$method;
        $form->process($query);
    }
    else {
        $form->process;
    }

    $c->stash->{css_id} = $css_id;
    
    $c->stash->{use_dojo} = 1 if $form_name =~ /dojo/;
    
    return;
}

sub with_errors : Private {
    return {
        text2 => 'a',
    };
}


=head1 AUTHOR

Carl Franks

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
